<?php
namespace jy_cong9fan;
defined('BY_JYA') or exit('error');
/*
	分红佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		$is_open=intval($this->setting['is_open']);
		if($is_open==0){
			return;
		}
		$this->order=$order;
		$member=$this->get('member',array('mid'=>$order['mid']),array('mid','is_agent','agent_id','level_id'));
		if($member['level_id']){
			$this->nowlevel=$this->getcolumn('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']),'level');
		}else{
			$this->nowlevel=0;
		}
		$this->up_fanred($member);
		//自购升级
		if($member['is_agent']){
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
		}
		if($this->order['fee']<=0){
			return;
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'ordersn'=>$this->order['id'],
				'price'=>$this->order['fee'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_price'=>$this->order['fee'],
				'commission_money'=>0,
				'fee2'=>0,
				'ispay'=>1,
				'createtime'=>TIMESTAMP,
		);
		$agent_id=$member['agent_id'];
		for($i=0;$i<12;$i++){
			if(!$agent_id){
				break;
			}
			$member=$this->get('member',array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
			$agent_id=$member['agent_id'];
			$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$this->create_commission($member,$i+1,$level);
			$this->nowlevel=$level['level'];
			//如果他的上级是我
			$ta_agent_id=$this->getcolumn('member',array('mid'=>$member['agent_id']),'agent_id');
			if(($agent_id>0 && $ta_agent_id==$agent_id) || $agent_id==$member['mid']){
				break;
			}
		}
		//生成分红订单
		$indata=$this->commission_order;
		unset($indata['fee2']);
		$this->insert('order',$indata);
		return true;
	}
	function create_commission($member,$commission_level=1,$level=array()){
		if($member && $member['is_agent'] && $member['stop']==0){
			//相应会员存在且是分销商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
				$level=$this->plugin_setting('default_level');
				$level_id=0;
			}else{
				$level_id=$level['id'];
			}
			if($level['level']==$this->nowlevel){
				$isping=1;
			}else{
				$isping=0;
			}
			$fee2=0;
			$fee3=0;
			$fee4=0;
			$order_price=$this->commission_order['commission_price'];
			$per2=$level['red_rate'];
			$per3=$level['ping_rate'];
			$per4=$level['jia_rate'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			if(substr($per2,-1)=='%'){
				$fee2=intval($order_price*substr($per2,0,-1))/100;
				$fee2 -=$this->commission_order['fee2'];
			}else{
				$fee2=$per2;
				$fee2 -=$this->commission_order['fee2'];
			}
			if($isping){
				if(substr($per3,-1)=='%'){
					$fee3=intval($order_price*substr($per3,0,-1))/100;
				}else{
					$fee3=$per3;
				}
			}
			if($commission_level>1){
				if(substr($per4,-1)=='%'){
					$fee4=intval($order_price*substr($per4,0,-1))/100;
				}else{
					$fee4=$per4;
				}
			}
			if($fee2>0){
				$this->commission_order['fee2']+=$fee2;
				$this->commission_order['commission_money']+=$fee2;
				$remark['commission_money']=$fee2;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['id'],
						'money'=>$fee2,
						'type'=>2,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>0,
				);
				$this->insert('commission',$new_commission);
			}
			if($fee3>0){
				$this->commission_order['commission_money']+=$fee3;
				$remark['commission_money']=$fee3;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['id'],
						'money'=>$fee3,
						'type'=>3,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>0,
				);
				$this->insert('commission',$new_commission);
			}
			if($fee4>0){
				$this->commission_order['commission_money']+=$fee4;
				$remark['commission_money']=$fee4;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['id'],
						'money'=>$fee4,
						'type'=>4,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>0,
				);
				$this->insert('commission',$new_commission);
			}
			$this->model('member')->update_agent_level($member['mid']);
		}
		return $member['agent_id'];
	}
	public function up_fanred($member=array()){
		$redfeeset=$this->setting['redfeeset'];
		$onefanfee=0;
		foreach($redfeeset as $rv){
			if($rv['fee']==$this->order['fee']){
				$fanfee=$rv['fanfee'];
				$onefanfee=$rv['onefanfee']*$rv['fanfee']/100;
				$jiafee=$rv['jiafee'];
				break;
			}
		}
		$fee1=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1),'money');
		$onefanfee +=$fee1;
		if($fanfee || $onefanfee){
			$fanlog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'orderid'=>$this->order['id'],
				'fanfee'=>$fanfee,
				'lastfanfee'=>$fanfee,
				'onefanfee'=>$onefanfee,
				'createtime'=>TIMESTAMP,
				'uptime'=>TIMESTAMP,
			);
			$this->insert('fanlog',$fanlog);
			$fanlog['id']=pdo_insertid();
			if($member['agent_id']){
				$this->up_fee1($fanlog,$jiafee,$member['agent_id']);
			}
			if(!$this->setting['fanday']){
				$this->up_fanone($fanlog);
			}
		}
		return true;
	}
	public function up_fee1($fanlog=array(),$jiafee=0,$agent_id=0){
		if($jiafee<=0){
			return false;
		}
		$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$agent_id,
						'level'=>1,
						'ordersn'=>$fanlog['orderid'],
						'money'=>$jiafee,
						'type'=>1,
						'createtime'=>TIMESTAMP,
						'status'=>0,
		);
		$this->insert('commission',$new_commission);
		return true;
	}
	public function up_fanone($fanlog=array()){
		if(!$fanlog['onefanfee'] || $fanlog['onefanfee']<=0){
			return false;
		}
		if($this->setting['fanday']){
			$createday=date('Y-m-d',$fanlog['createtime']);
			$createtime=strtotime($createday);
			$fantime=$this->setting['fanday']*24*3600+$createtime;
			if($fantime>TIMESTAMP){
				return false;
			}
		}
		$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$fanlog['mid'],
						'level'=>0,
						'ordersn'=>$fanlog['orderid'],
						'money'=>$fanlog['onefanfee'],
						'type'=>6,
						'createtime'=>TIMESTAMP,
						'status'=>0,
		);
		$this->insert('commission',$new_commission);
		cfc('core')->member_credit($fanlog['mid'],'credit2',$fanlog['onefanfee'],1,'jy_cong9fan','充值周期返额');
		if($fanlog['id']){
			$update=array('uptime'=>TIMESTAMP);
			if($fanlog['onefanfee']>=$fanlog['lastfanfee']){
				$fanlog['onefanfee']=$fanlog['lastfanfee'];
				$update['status']=1;
				$update['lastfanfee']=0;
			}else{
				$update['lastfanfee -=']=$fanlog['onefanfee'];
			}
			pdo_update('jy_cong9fan_fanlog',$update,array('id'=>$fanlog['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
}